package com.yuezhi.consumer.config.Publish_Subscribe;

import com.yuezhi.consumer.constant.QueueConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitHeaderConfig {

    @Bean
    Queue headerOne() {
        return new Queue(QueueConstant.HEADER_QUEUE_ONE);
    }

    @Bean
    Queue headerTwo() {
        return new Queue(QueueConstant.HEADER_QUEUE_TWO);
    }

    @Bean
    HeadersExchange headersExchange() {
        return new HeadersExchange(QueueConstant.HEADER_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingHeaderOne() {
        Map<String, Object> map = new HashMap<>();
        // 消息头部中必须包含 headerOne 属性,且属性值为 ok
        map.put("headerOne","ok");
        return BindingBuilder.bind(headerOne()).to(headersExchange()).whereAny(map).match();
    }

   @Bean
    Binding bindingHeaderTwo() {
        return BindingBuilder.bind(headerTwo())
                .to(headersExchange())
                // 如果将来消息头部中包含 headerTwo 属性，就算匹配成功
                .where("headerTwo").exists();
    }
}
